package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.CommBean;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: 社区主播头像adapter
 * @Description: java类作用描述
 * @Author: ljp
 * @CreateDate: 2018/9/4 14:11
 * @Version: 1.0
 */
public class AnchorIconAdapter extends BaseAdapter<CommBean.MapBean.RealTimeBean>{
    private Context context;
    private  List<CommBean.MapBean.RealTimeBean> tlist;
    int number = 0;
    Thread thread;
    private OnClick onClick;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1){
                Bundle data = msg.getData();
                byte[] bitmap = data.getByteArray("bitmap");
                CircleImageView view = (CircleImageView) msg.obj;
                GlideLoadUtils.getInstance().glideLoad(context,bitmap,view);
                /*GlideApp.with(context)
                        .load(bitmap)
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(view);*/
            }
        }
    };
    private String TAG = "AnchorIconAdapter";

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }
    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public AnchorIconAdapter(Context context, List<CommBean.MapBean.RealTimeBean> tlist, int mLayout) {
        super(context, tlist, mLayout);
        this.context = context;
        this.tlist = tlist;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        ViewHolder1 viewHolder1 = new ViewHolder1(view);

        return viewHolder1;
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
       if(viewHolder instanceof ViewHolder1){
           Log.i(TAG, "onBindViewHolder: --------"+tlist.get(position).getBitmap());
           //setImage(tlist.get(position).getAvatarUrl(),((ViewHolder1) viewHolder).imageIcon);//下载头像
           if (!tlist.get(position).equals(((ViewHolder1) viewHolder).imageIcon.getTag(R.id.circle_picture))) {
               // 加载图片
               ((ViewHolder1) viewHolder).imageIcon.setTag(R.id.circle_picture, tlist.get(position).getAvatarUrl());
               GlideLoadUtils.getInstance().glideLoad(context,OssConfig.OSS_HTTP+tlist.get(position).getAvatarUrl(),
                       ((ViewHolder1) viewHolder).imageIcon);
               //setImage(dynamic.getAvatarUrl(), headViewHolder.imageIcon);
           }
           ((ViewHolder1) viewHolder).imageIcon.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   onClick.onSetPictureClick(((ViewHolder1) viewHolder).imageIcon,position);
               }
           });
           String substring = tlist.get(position).getBirthday().substring(0, 4);
           String time = Utils.getStringTime(new Date());
           int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
           ((ViewHolder1) viewHolder).tvAge.setText(age+"");
        }
    }
    private void setImage(String url,View view){
        OssClient.getInstance().init(context).fileDownload(url, new OssClient.OnDownloadFile() {
            @Override
            public void onDownFileSuccess(InputStream inputStream) {
                byte[] bitmap = null;
                try {
                    bitmap = OssConfig.readInputStreamToByte(inputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Message message = Message.obtain();
                message.what = 1;
                message.obj = view;
                Bundle bundle = new Bundle();
                bundle.putByteArray("bitmap",bitmap);
                message.setData(bundle);
                handler.sendMessage(message);
            }

            @Override
            public void onDownFileProgress(String progress) {

            }
        });
    }

    /**
     * 布局1
     */
    class ViewHolder1 extends BaseViewHolder{
        @BindView(R.id.circle_picture)
        CircleImageView imageIcon;
        @BindView(R.id.tv_age)
        TextView tvAge;
        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public  interface OnClick{
        void onSetPictureClick(View view, int position);
    }
}
